package list.model;

import list.control.MysqlLink;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by ZhipengLi.
 */
public class User extends MysqlLink {
    private static final String TABLE = "user";

    private int user_id;

    private String id;

    private String email;

    private String birthday;

    private String head;

    private String phone;

    private Tag[] tags;

    private Contact[] contacts;

    public User() {
    }

    public User(int user_id) {
        this.user_id = user_id;
    }

    public User(String id, String phone, int user_id, String birthday, String head) {
        this(user_id);
        this.id = id;
        this.phone = phone;
        this.birthday = birthday;
        this.head = head;
    }

    public User(String idOrEmail) {
        this.init(idOrEmail);
    }

    public static boolean login(String idOrPhone, String password) {
        ResultSet resultSet = select(TABLE, ALL, equalsOr(toArray("id", "phone"), toArray(idOrPhone, idOrPhone)));
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    String true_password = resultSet.getString("password");
                    resultSet.close();
                    /*if (true_password.equals(password)) {
                        int user_id = resultSet.getInt("user_id");
                        String id = resultSet.getString("id");
                        String phone = resultSet.getString("phone");
                        String birthday = resultSet.getString("birthday");
                        String head = resultSet.getString("head");
                        User user = new User(id, phone, user_id, birthday, head);
                        user.tags = Tag.select(user_id);
                    }*/
                    return true_password.equals(password);
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean register(String id, String email, String password) {
        return !isExisted(id, email) && insert(TABLE, toArray("id", "email", "password"), toArray(id, email, password));
    }

    private static boolean isExisted(String id, String email) {
        return exist(TABLE, "id", equalsOr(toArray("id", "email"), toArray(id, email)));
    }

    public void init() {
        ResultSet resultSet = select(TABLE, ALL, equal("user_id", user_id));
        init(resultSet);
    }

    public boolean init(String idOrEmail) {
        ResultSet resultSet = select(TABLE, ALL, equalsOr(toArray("id", "email"), toArray(idOrEmail, idOrEmail)));
        return init(resultSet);
    }

    private boolean init(ResultSet resultSet) {
        boolean result = true;
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    this.user_id = resultSet.getInt("user_id");
                    this.id = resultSet.getString("id");
                    this.email = resultSet.getString("email");
                    this.phone = resultSet.getString("phone");
                    this.birthday = resultSet.getString("birthday");
                    this.head = resultSet.getString("head");
                } else {
                    result = false;
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public boolean updatePassword(String old_password, String new_password) {
        return update(TABLE, "password", new_password, equalsAnd(toArray("user_id", "password"), toArray(user_id, old_password)));
    }

    public boolean updateField(String key, Object value) {
        String[] fields = {"email", "name", "birthday", "head", "phone"};
        for (String field : fields) {
            if (field.equals(key)) {
                return update(TABLE, key, value, equal("user_id", user_id));
            }
        }
        return false;
    }

    public boolean updateAll(String email, String birthday, String phone) {
        return update(TABLE, toArray("email", "birthday", "phone"), toArray(email, birthday, phone), equal("user_id", user_id));
    }

    public boolean insertContact(int tag_id) {
        return Contact.insert(user_id, tag_id);
    }

    public boolean deleteContact(int tag_id, int contact_id) {
        return Contact.delete(user_id, tag_id, contact_id) && Telephone.delete(contact_id);
    }

    public boolean updateContact(int contact_id, String property, Object value) {
        return Contact.update(user_id, contact_id, property, value);
    }

    public boolean updateContact(int contact_id, String nickname, String birthday, String head, String work_place, String postcode, String wechat, String qq, String alipay) {
        return Contact.update(contact_id, nickname, birthday, head, work_place, postcode, wechat, qq, alipay);
    }

    public Contact[] getContacts(int tag_id) {
        if (null == this.contacts) {
            this.contacts = Contact.select(user_id, tag_id);
        }
        return this.contacts;
    }

    public Contact[] getContacts() {
        return Contact.select(user_id);
    }


    public int getNumOfContacts() {
        if (null == this.getTags()) {
            return 0;
        }
        int count = 0;
        for (Tag tag : this.getTags()) {
            count += tag.getCount();
        }
        return count;
    }

    public boolean insertTag() {
        return Tag.insert(user_id);
    }

    public boolean deleteTag(int tag_id) {
        Contact.delete(tag_id);
        return Tag.delete(user_id, tag_id);
    }

    public boolean updateTag(int tag_id, String tag_name) {
        return Tag.update(user_id, tag_id, tag_name);
    }

    private void selectTag() {
        this.tags = Tag.select(user_id);
    }

    public int getUser_id() {
        return user_id;
    }

    public String getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getHead() {
        return head;
    }

    public String getPhone() {
        return phone;
    }

    public Tag[] getTags() {
        if (null == this.tags) {
            this.selectTag();
        }
        return tags;
    }
}
